package com.cqndt.disaster.device.controller;

import java.util.Map;
import org.slf4j.Logger;
import java.util.HashMap;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpSession;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import javax.servlet.http.HttpServletRequest;
import com.cqndt.disaster.device.util.Result;
import com.cqndt.disaster.device.util.AjaxResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.cqndt.disaster.device.service.AppWarnStatisticsService;

/**
 * @author lgb
 * @date 2020-9-1
 * @return 预警统计
 **/
@RestController
@RequestMapping(value="/api/v2")
public class WarnStatisticsController extends BaseController{
	
    Logger logger = LoggerFactory.getLogger(WarnStatisticsController.class);
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private AppWarnStatisticsService warnStatisticsService;

    /**
     * @param request
     * @param areaCode 区域编码
     * @param level 区域等级
     * @param type 1:当天、2：本周、3：当月
     * @return 查询各预警等级的占比
     **/
    @PostMapping(value="/warnStatics")
    public AjaxResult warnStatictis(HttpServletRequest request,String areaCode,
    		String level,String type){
        Map<String,Object> map=new HashMap<String, Object>();
        String userId = request.getHeader( "id" );
        map.put("areaCode",null == areaCode?stringRedisTemplate.opsForValue().get("areaCode" +userId):areaCode);
        map.put("areaLevel",null == level?stringRedisTemplate.opsForValue().get("level" +userId):level);
        map.put( "type",type );
        map.put( "userId",userId );
        return entityResult(warnStatisticsService.findWarnStatictics(map),"获取各预警等级的占比");
    }

    /**
     * @param areaCode 区域编码
     * @param level 区域等级
     * @param type 1:当天、2：本周、3：当月
     * @param warn_type 告警类型
     * @param warn_level 告警等级
     * @return 查询预警具体信息
     **/
    @PostMapping(value="/findWarnMsg")
    public Result findWarnMsg(String areaCode,String level,String type,Integer page,
    		Integer limit,HttpServletRequest request,String warn_type,String warn_level){
        Map<String,Object> map=new HashMap<String, Object>();
        String userId = request.getHeader( "id" );
        map.put("areaCode",null == areaCode?stringRedisTemplate.opsForValue().get("areaCode" +userId):areaCode);
        map.put("areaLevel",null == level?stringRedisTemplate.opsForValue().get("level" +userId):level);
        map.put("userId",userId);
        map.put("type",type);
        map.put("warn_type",warn_type);
        map.put("warn_level",warn_level);
        PageHelper.startPage(page,limit);
        PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(warnStatisticsService.findWarnMsg(map));
        Result result = new Result();
        result.setCode(200);
        result.setData(pageInfo.getList());
        return result;
    }
    /**
     * @param areaCode 区域编码
     * @param level 区域等级
     * @param type 1:当天、2：本周、3：当月
     * @return 查询各告警数和总数
     **/
    @PostMapping(value="/findWarnCount")
    public AjaxResult findWarnCount(String areaCode,String level,
    		String type,HttpSession session,HttpServletRequest request){
        Map<String,Object> map=new HashMap<String, Object>();
        String userId = request.getHeader( "id" );
        map.put("areaCode",null == areaCode?stringRedisTemplate.opsForValue().get("areaCode" +userId):areaCode);
        map.put("areaLevel",null == level?stringRedisTemplate.opsForValue().get("level" +userId):level);
        map.put("userId",userId);
        map.put("type",type);
        return entityResult(warnStatisticsService.findWarnCount(map),"查询预警具体信息");
    }
}